package com.jerry.provider.controller;

import com.jerry.common.bean.Order;
import com.jerry.provider.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private DiscoveryClient client;

    //查询订单列表
    @GetMapping("/list")
    public List<Order> list() {
        ServiceInstance instance = client.getLocalServiceInstance();
        List<Order> orderList = orderService.list();
        for (Order order : orderList) {
            order.setHost(instance.getHost());
            order.setPort(instance.getPort());
            order.setServerId(instance.getServiceId());
            System.out.println("provider === order === "+order);
        }
        return orderList;
    }

    //根据orderNo查询订单
    @GetMapping("/get/{orderNumber}")
    public Order get(@PathVariable("orderNumber") String orderNumber) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Order order = orderService.get(orderNumber);
        order.setHost(instance.getHost());
        order.setPort(instance.getPort());
        order.setServerId(instance.getServiceId());
        System.out.println("provider === order === "+order);
        return order;
    }

    //根据user_id查询订单列表
    @GetMapping("/getByUserId/{userId}")
    public List<Order> getByUserId(@PathVariable("userId") long userId) {
        ServiceInstance instance = client.getLocalServiceInstance();
        List<Order> orderList = orderService.getByUserId(userId);
        for (Order order : orderList) {
            order.setHost(instance.getHost());
            order.setPort(instance.getPort());
            order.setServerId(instance.getServiceId());
            System.out.println("provider === order === "+order);
        }
        return orderList;
    }

}
